/*******************************************************************************
 * Copyright (c) 2010 Marcelo Cataldo, Sangeeth Nambiar
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * PropertyVewierPart.java
 * 
 * Properties view, which is a table viewer in which the properties of the
 * element selected in the project explorer are displayed. This class also
 * installs a listener in the project explorer to detect which object is selected
 * in the project explorer and then displays it's properties in the table.
 * 
 * This class also constains the content and label providers for the table viewer
 * 
 * Contributors:
 *    Marcelo Cataldo  - initial implementation 
 *    Sangeeth Nambiar - initial implementation
 *******************************************************************************/
package edu.cmu.isr.dinvenio.ui.views;

import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TreeSelection;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.ui.ISelectionListener;
import org.eclipse.ui.ISelectionService;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.part.ViewPart;

import edu.cmu.isr.dinvenio.Activator;
import edu.cmu.isr.dinvenio.model.DepsGraphNode;
import edu.cmu.isr.dinvenio.model.DepsGraphNodeSet;
import edu.cmu.isr.dinvenio.model.DepsGraphProperty;

public class PropertyVewierPart extends ViewPart implements ISelectionListener{
	private TableViewer tableViewer;

	public PropertyVewierPart() {
		// TODO Auto-generated constructor stub
	}

	@Override
	public void createPartControl(Composite parent) {
		tableViewer = new TableViewer(parent, SWT.BORDER | SWT.H_SCROLL | SWT.FULL_SELECTION | SWT.V_SCROLL);
		final Table table = tableViewer.getTable();
		
		GridData gridData = new GridData(GridData.GRAB_HORIZONTAL | GridData.GRAB_VERTICAL | GridData.FILL_BOTH);
		table.setLayoutData(gridData);

		table.setHeaderVisible(true);
		table.setLinesVisible(true);
		/***********************************************************************
		 * register the properties view as selection listener
		 **********************************************************************/
		ISelectionService service = getSite().getWorkbenchWindow().getSelectionService();
		service.addSelectionListener(this);
		
		/***********************************************************************
		 * create the headers with specific viewer comparators
		 **********************************************************************/
		TableColumn property = new TableColumn(table, SWT.LEFT);
		property.setText("Property");
		property.setWidth(80);

		TableColumn source = new TableColumn(table, SWT.LEFT);
		source.setText("Value");
		source.setWidth(80);
		
		/***********************************************************************
		 * setting the content and label provider
		 **********************************************************************/
		tableViewer.setContentProvider(new PropertyViewContentProvider());
		tableViewer.setLabelProvider(new PropertyViewLabelProvider());
		getSite().setSelectionProvider(tableViewer);
	}

	@Override
	public void setFocus() {		
	}
	public void setPropertyTableValues(){
		// TODO The method has to be completed
	}

	@Override
	public void selectionChanged(IWorkbenchPart part, ISelection selection) {
		if(part instanceof ProjectViewPart){
			if(((TreeSelection)selection).getFirstElement() != null){
				tableViewer.setInput(((TreeSelection)selection).getFirstElement());
			}
		}
	}
	
	@Override
	public void dispose() {
		ISelectionService s = getSite().getWorkbenchWindow().getSelectionService();
		s.removeSelectionListener(this);
		super.dispose();
	}

	class PropertyViewContentProvider implements IStructuredContentProvider {
		@Override
		public Object[] getElements(Object iE) {			
			if(iE instanceof DepsGraphNode){
				return ((DepsGraphNode)iE).getDepsGraphProperties().values().toArray();
			}
			else{
				Object[] objs = {iE};
				return objs;
			}
		}

		@Override
		public void dispose() {
			// TODO Auto-generated method stub
		}

		@Override
		public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
			// TODO Auto-generated method stub
		}

	}

	class PropertyViewLabelProvider extends LabelProvider implements ITableLabelProvider {

		/* (non-Javadoc)
		 * @see org.eclipse.jface.viewers.ITableLabelProvider#getColumnImage(java.lang.Object, int)
		 */
		@Override
		public Image getColumnImage(Object element, int columnIndex) {
			// TODO Auto-generated method stub
			return null;
		}

		/* (non-Javadoc)
		 * @see org.eclipse.jface.viewers.ITableLabelProvider#getColumnText(java.lang.Object, int)
		 */
		@Override
		public String getColumnText(Object element, int columnIndex) {
			if(element instanceof DepsGraphProperty) {
				switch (columnIndex) {
				case 0: {return ((DepsGraphProperty)element).getName();	}
				case 1: {return ((DepsGraphProperty)element).getValue();}
				}
			}
			else if(element.equals("Graph")) {
				switch (columnIndex) {
				case 0: {return "Node Sets";	}
				case 1: {return String.valueOf(Activator.myController.getGraphModel().nodeSets.size());}
				}
			}
			else if(element instanceof DepsGraphNodeSet) {
				switch (columnIndex) {
				case 0: {return "Nodes";	}
				case 1: {return String.valueOf(((DepsGraphNodeSet)element).nodes.size());}
				}
			}
			return "";
		}
	}

}
